package com.suncaper.han_yi_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.suncaper.han_yi_system.pojo.entity.Student;
import com.suncaper.han_yi_system.result.Result;
import com.suncaper.han_yi_system.service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Objects;

@RestController
@RequestMapping("/student")
@CrossOrigin(origins = "*")
@Tag(name = "学生管理")
@Slf4j
public class StudentController {

    @Autowired
    StudentService studentService;


    @Operation(summary = "新增学生信息")
    @PostMapping("/save")
    public Result save(@RequestBody Student student){
        student.setCreateTime(LocalDateTime.now());
        studentService.save(student);
        return Result.ok();
    }

    @Operation(summary = "更新学生信息")
    @PutMapping("/update")
    public Result update(@RequestBody Student student){
        student.setUpdateTime(LocalDateTime.now());
        studentService.updateById(student);
        return Result.ok();
    }

    @Operation(summary = "删除学生信息")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        if(Objects.isNull(id)){
            return Result.error().message("id不能为空");
        }
        studentService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id获取学生信息")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Long id){
        if(Objects.isNull(id)){
            return Result.error().message("id不能为空");
        }
        return Result.ok().data("student",studentService.getById(id));
    }

    @Operation(summary = "根据用户id获取学生信息")
    @GetMapping("/getByUserId/{id}")
    public Result getByUserId(@PathVariable Long id){
        if(Objects.isNull(id)){
            return Result.error().message("id不能为空");
        }
        Student student = studentService.getOne(new QueryWrapper<Student>().eq("account", id));
        return Result.ok().data("student",student);
    }

    @Operation(summary = "获取学生列表")
    @RequestMapping("/list")
    public Result getList(){
        return Result.ok().data("studentList",studentService.list());
    }

}